package com.zqwm.fly.utils;

import org.htmlparser.Node;
import org.htmlparser.NodeFilter;
import org.htmlparser.Parser;
import org.htmlparser.filters.TagNameFilter;
import org.htmlparser.tags.ImageTag;
import org.htmlparser.util.NodeList;

public class HtmlParserUtil
{
    public static String changeImgSrc(String htmlPath)
    {
        String result=null;
        try {
            Parser parser = new Parser(htmlPath);
            NodeFilter filter = new TagNameFilter("img");
            NodeList nodes = parser.extractAllNodesThatMatch(filter);
            Node eachNode = null;
            ImageTag imageTag = null;
            String oldSrcPath = null;
            if (nodes != null)
            {
//              遍历所有的img节点
                for (int i = 0; i < nodes.size(); i++)
                {
                    eachNode = (Node)nodes.elementAt(i);
                    if (eachNode instanceof ImageTag)
                    {
                        imageTag = (ImageTag)eachNode;

//                      获得html文本的原来的src属性
                        oldSrcPath = imageTag.getAttribute("src");
                        result = oldSrcPath;
                        break;
//                      setImageURL 貌似没法在本地进行保存
                        //imageTag.setImageURL(addSrcPath + oldSrcPath);
                    }
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

        return result;
    }
}
